import axios from 'axios'

const http = axios.create({
    baseURL: 'http://localhost:3000/'
})
function ajax(url, method = "GET", data) {
    method = method.toUpperCase()
    return new Promise((resolve, reject) => {
        let promise
        if(method === 'GET') {
          promise = http.get(url, {
                params: data
            })
        }
        if(method === "POST") {
            promise = http.post(url, data)
        }
        if(method === "PUT") {
            promise = http.put(url, data)
        }
        if(method === "DELETE") {
            promise = http.delete(url)
        }
        return promise.then(res => {
            resolve(res.data)
        }).catch(error => {
            reject(error)
        })
    })
}

export default ajax